import * as THREE from "three";

let binStateType=[{
  type:'',
  object: ''
}]//用来存储货物对象的类型


/**
 *
 * @param group 当前货物所在组的对象
 * @param bin 当前货位对象
 * @param number 该货物对象是否被创建过
 * @constructor
 */
export const Goods=(group,bin,number)=>{
  //列号
  let col=bin.col;
  //层号
  let layer=bin.layer;
  /*
          支架的高=最底层的高度+(库位数-1)*库位的高度
       */
  let shelfHeight=group.botton_height+(group.bin_ynum)*group.bin_height;
  /*
   支架的宽=库位数乘以库位的宽度
   */
  let shelfWidth=group.bin_znum*group.bin_width;

  let positionX=group.position.x;
  let positionY=group.position.y-shelfHeight/2+group.bin_height/2+(layer-1)*group.bin_height+group.botton_height;
  let positionZ=group.position.z+shelfWidth/2-group.bin_width/2-(col-1)*group.bin_width;
  let no=bin.no;
  let name=bin.bin_name;
  let length=group.bin_length-6;
  let width=group.bin_width-6;
  let height=group.bin_height-6;
  let color;
  let opacity;

  if(bin.state===1)
  {
    color=0x46C191;
    opacity=0.8;
  }
  else if(bin.state===2)
  {
    color=0xF0CD3F;
    opacity=0.8;
  }
  else if(bin.state===3)
  {
    color=0xF55E35;
    opacity=0.8;
  }
  else
  {
    color=0x46C191;
    opacity=0.1;
  }

  //==============
  //根据number判断是否需要新创建几何体
  if (number==null){
    //要新创建
    let boxGeometry = new THREE.BoxGeometry(length,height,width);
    let meshPhongMaterial = new THREE.MeshPhongMaterial({
      color: color,
      transparent: true,
      depthTest: true,
      opacity: opacity
    });
    let mesh = new THREE.Mesh(boxGeometry,meshPhongMaterial);
    binStateType.push({
      type: bin.state,
      object: mesh
    });
    mesh.name=name;
    mesh.uuid=no;
    mesh.position.x=positionX;
    mesh.position.y=positionY;
    mesh.position.z=positionZ;
    return mesh;
  }else {
    //使用clone
    let clone = number.clone();
    clone.position.set(positionX,positionY,positionZ);
    clone.uuid=no;
    clone.name=name;
    return clone;
  }
}

//判断该类型的几何体对象是否创建过
export const judgmentObject=(state)=>{
  for (var i=0;i<binStateType.length;i++){
    let type = binStateType[i];
    if (state===type.type){
      //已经生成过该类型的对象几何体了
      return type.object;
    }
  }
  //没有生成过该类型的几何体对象
  return null;
}
